<?php

namespace  app\controller;


use app\BaseController;
use app\model\PostsComment;
use app\model\PostsHot;
use app\model\PostsInfo;
use app\model\User;
use app\model\UserPostsLike;
use app\Request;
use think\facade\Db;
use think\Filesystem;

class  UserController extends BaseController{

    /**
     * 查询个人信息
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public  function getUser(Request $request){
        $uid = $request->user_id;
        $findData = ['id','head_img','student_id','name','sex','phone','email'];
        $getData = User::where('id','=',$uid)->field($findData)->select();
        return $this->returnJson(self::SUCCESS,'成功',$getData);
    }

    /**
     * 修改个人信息
     * @param Request $request
     * @return mixed
     */
    public  function  updateUser(Request $request)
    {
        $uid = $request->user_id;
        $getData = $request->param(['phone', 'email']);
        $isUpdate = User::where('id', '=', $uid)->update([
            'phone' => $getData['phone'],
            'email' => $getData['email'],
            
        ]);

            if ($isUpdate != 1) {
                return $this->returnJson(self::USER_ERROR, '修改个人信息失败', $isUpdate);
            }
            return $this->returnJson(self::SUCCESS, '修改个人信息成功', '');

    }
    /**
     * 修改头像
     * @param Request $request
     * @return mixed
     */
        public function updateHeadImg(Request $request){
            $uid = $request->user_id;
            $getData = $request->param(['head_img']);
            $isUpdate = User::where('id','=',$uid)->update([
                'head_img'        =>  $getData['head_img'],
            ]);
            if ($isUpdate != 1){
                return $this->returnJson(self::USER_ERROR,'失败','');
            }
            return $this->returnJson(self::SUCCESS,'成功','');
        }

    /**
     * 修改背景
     * @param Request $request
     * @return mixed
     */
    public function updateBgImg(Request $request){
        $uid = $request->user_id;
        $getData = $request->param(['bg_img']);
        $isUpdate = User::where('id','=',$uid)->update([
            'bg_img'        =>  $getData['bg_img'],
        ]);
        if ($isUpdate != 1){
            return $this->returnJson(self::USER_ERROR,'失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功','');
    }

    /**
     * 验证且修改密码
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public  function  updatePassword(Request $request)
    {
        $uid = $request->user_id;
        $getData = $request->put();
        $msg='';//新建一个返回信息字符串
        if (!empty($getData['pwd'])){
            $this->validate($getData,'UserValidate.updatePwd');

            if (($getData['oldPwd']) != User::where('id','=',$uid)->find()['password']){
                return $this->returnJson(self::USER_ERROR,'该密码与原密码不匹配');
            }
            if (($getData['pwd']) == User::where('id','=',$uid)->find()['password']){
                return $this->returnJson(self::USER_ERROR,'新密码不能和原密码相同');
            }

        }
        User::where('id','=',$uid)->update([
            'password'=>($getData['pwd'])
        ]);
        $msg = '密码';
        return $this->returnJson(self::SUCCESS,'修改'.$msg.'成功');
    }

    /**
     * 查询帖子
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPost(Request $request){
        $uid = $request->user_id;
        $findData = ['id','class_id','user_id','post_title','post_content','insert_time','delete_time','number_comments'];
        $getData = PostsInfo::where('id','=',$uid)->field($findData)->select();
        return $this->returnJson(self::SUCCESS,'成功',$getData);
    }

    /**
     * 删除帖子
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public  function  droPost(Request $request){
        $uid = $request->user_id;
        $getData = Db::table('posts_info')
            ->where('id',$uid)
            ->useSoftDelete('delete_time',time())
            ->delete();
        if ($getData != 1){

            return $this->returnJson(self::USER_ERROR,'失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功','');
    }


    /**
     * 收藏夹
     * 多表查询
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPostsLike(Request $request){
        $uid = $request->user_id;
        $post_id = UserPostsLike::where('user_id','=',$uid)->where('is_collection','=',1)->field(['post_id'])->select();
        $data = [];
        foreach ($post_id as $k => $v){
            $post_info = PostsInfo::where('id','=',$v['post_id'])->field(['post_title','post_content','post_img'])->select();
            $comment_num = PostsComment::where('post_id','=',$v['post_id'])->count();
            $post_hot = PostsHot::where('post_id','=',$v['post_id'])->value('hot');
            foreach ($post_info as $key => $value){
                $value['comment'] = $comment_num;
                $value['hot'] = $post_hot;
            }
            array_push($data,$post_info);

        }
        return $this->returnJson(self::SUCCESS,'成功',$data);
    }

    /**
     * 取消收藏
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DbException
     */
    public  function  unCollection(Request $request)
    {
        $uid = $request->user_id;
        $getData = $request->param(['post_id']);
        $isUpdate = UserPostsLike::where('user_id','=',$uid)->where('post_id','=',$getData['post_id'])->update([
            'is_collection' => 0
        ]);
        if ($isUpdate != 1){
            return $this->returnJson(self::USER_ERROR,'失败','');
        }
        return $this->returnJson(self::SUCCESS,'成功','');
    }
}